import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold_ohos/getx_scaffold_ohos.dart';

import '../../../common/index.dart';
import 'permission_logic.dart';

class PermissionPage extends StatelessWidget {
  PermissionPage({super.key});

  final PermissionLogic logic = Get.put(PermissionLogic());

  // 主视图
  Widget _buildView() {
    return <Widget>[
      ListTile(
        title: const Text('申请相机权限'),
        onTap: () async {
          if (await requestCameraPermission()) {
            showSuccessToast('已获取相机权限');
          }
        },
      ),
      ListTile(
        title: const Text('申请相册权限'),
        onTap: () async {
          if (await requestPhotosPermission()) {
            showSuccessToast('已获取相册权限');
          }
        },
      ).visibility(visible: !PlatformUtil.isOhos),
      ListTile(
        title: const Text('申请麦克风权限'),
        onTap: () async {
          var result = await requestPermission(
            permission: Permission.microphone,
            message: '我们申请使用您设备的麦克风权限，用于拍摄录音',
            error: '请授权麦克风权限',
          );
          if (result) {
            showSuccessToast('已获取麦克风权限');
          }
        },
      ),
    ].toListView(
      separator: const DividerX(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PermissionLogic>(
        id: 'permission',
        builder: (logic) {
          return Scaffold(
            appBar:
                AppBar(title: Text(TextKey.shenQingQuanXian.tr), elevation: 1),
            body: SafeArea(
              child: _buildView(),
            ),
          );
        });
  }
}
